package com.leizi.igym.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.leizi.igym.constant.EquipConstant;
import com.leizi.igym.constant.TrainerConstant;
import com.leizi.igym.enums.ReturnCodeEnum;
import com.leizi.igym.exception.BusinessException;
import org.apache.commons.lang3.ObjectUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Objects;

/**
 * @Author: leizi0110
 * @CreateTime: 2024-04-07  16:52
 * @Description:
 */

public class DateQWUtils {

    public static boolean dateCompare(LocalDate beginTime, LocalDate endTime) {
        if (ObjectUtils.allNotNull(beginTime, endTime)){
            return endTime.isAfter(beginTime);
        }
        return true;
    }

    public static <T> QueryWrapper<T> getDateQWForTrainer(QueryWrapper<T> qw, LocalDate beginTime, LocalDate endTime) {
        if (!dateCompare(beginTime, endTime)) {
            throw new BusinessException(ReturnCodeEnum.PARAMS_ERROR, "传入的日期有误！");
        }
        if (Objects.nonNull(beginTime)) {
            qw.ge(TrainerConstant.TABLE_TRAINER_ALIAS + "." + "join_time", beginTime);
        }
        if (Objects.nonNull(endTime)) {
            qw.le(TrainerConstant.TABLE_TRAINER_ALIAS + "." + "join_time", endTime);
        }
        return qw;
    }

    public static <T> QueryWrapper<T> getDateQWForEquip(QueryWrapper<T> qw, LocalDate beginTime, LocalDate endTime) {
        if (!dateCompare(beginTime, endTime)) {
            throw new BusinessException(ReturnCodeEnum.PARAMS_ERROR, "传入的日期有误！");
        }
        if (Objects.nonNull(beginTime)) {
            qw.ge(EquipConstant.TABLE_EQUIP_ALIAS + "." + "create_time", LocalDateTime.of(beginTime, LocalTime.MIN));
        }
        if (Objects.nonNull(endTime)) {
            qw.le(EquipConstant.TABLE_EQUIP_ALIAS + "." + "create_time", LocalDateTime.of(endTime, LocalTime.MAX));
        }
        return qw;
    }

    public static <T> QueryWrapper<T> getDateQWForEmp(QueryWrapper<T> qw, LocalDate beginTime, LocalDate endTime) {
        if (!dateCompare(beginTime, endTime)) {
            throw new BusinessException(ReturnCodeEnum.PARAMS_ERROR, "传入的日期有误！");
        }
        if (Objects.nonNull(beginTime)) {
            qw.ge( "create_time", LocalDateTime.of(beginTime, LocalTime.MIN));
        }
        if (Objects.nonNull(endTime)) {
            qw.le( "create_time", LocalDateTime.of(endTime, LocalTime.MAX));
        }
        return qw;
    }
}
